Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new function set_content_image(), deprecate old set_image_* functions #303

Merged
merged 37 commits into from
Oct 8, 2024

Conversation

toph-allen
Copy link
Collaborator

@toph-allen toph-allen commented Sep 6, 2024

Fixes #294
Fixes #304

  • Deprecate set_image_path(content, path), set_image_url(content, url), set_image_webshot(content).
  • Add a new function, set_content_image(content, path). The new function can handle a local file path or a remote URL. If the path points to a local file that exists, it will use that. Otherwise, if it is an https or http URL, it will attempt to download that image to a temporary file and use that (the old behavior of set_image_url().

In a future PR to the connect repo, set_image_webshot() will become a recipe in the SDK Cookbook.

Note: this wasn't based off the 0.3.0 release, so the NEWS will have a merge conflict once that PR has merged. Not sure why I started it from main, but I did 🤷🏻.

Replaces #302, which I accidentally closed with a force push.

NEWS.md Outdated Show resolved Hide resolved
R/deploy.R Outdated Show resolved Hide resolved
R/deploy.R Outdated Show resolved Hide resolved
R/deploy.R Outdated Show resolved Hide resolved
R/deploy.R Outdated Show resolved Hide resolved
Copy link
Collaborator

@nealrichardson nealrichardson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement here! Here are some suggestions/+1s.

NEWS.md Outdated Show resolved Hide resolved
R/deploy.R Outdated Show resolved Hide resolved
R/deploy.R Outdated Show resolved Hide resolved
R/deploy.R Outdated
stop("Could not locate image at `path`")
}

guid <- content$get_content()$guid
con <- content$get_connect()

res <- con$POST(
path = unversioned_url("applications", guid, "image"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above, we should try the new v1 location, and if that returns 404, then POST to here.

R/deploy.R Outdated Show resolved Hide resolved
tests/integrated/test-deploy.R Outdated Show resolved Hide resolved
@toph-allen
Copy link
Collaborator Author

toph-allen commented Sep 9, 2024

Added first pass at supporting the v1 URLs. The exact conditional flow differs between different uses, because some of the calls include parsing the httr response object, but not all.

Will want to consider how to generalize this.

R/deploy.R Outdated Show resolved Hide resolved
R/deploy.R Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
NEWS.md Outdated Show resolved Hide resolved
R/thumbnail.R Show resolved Hide resolved
R/thumbnail.R Show resolved Hide resolved
R/thumbnail.R Outdated Show resolved Hide resolved
R/thumbnail.R Outdated Show resolved Hide resolved
R/thumbnail.R Outdated Show resolved Hide resolved
R/thumbnail.R Outdated Show resolved Hide resolved
R/thumbnail.R Outdated Show resolved Hide resolved
R/thumbnail.R Outdated Show resolved Hide resolved
R/thumbnail.R Outdated Show resolved Hide resolved
@toph-allen
Copy link
Collaborator Author

Back from vacation and assessing the scope of this PR, I'm going to hold off on implementing changes to the Connect class to do with server settings, checking the Connect version, memoizing failed requests to new endpoint versions; those will come in a future pull request.

README.Rmd Outdated Show resolved Hide resolved
Copy link
Contributor

@jonkeane jonkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack, meant to send these this morning, but apparently hadn't pressed the button

R/thumbnail.R Outdated Show resolved Hide resolved
tests/testthat/test-thumbnail.R Outdated Show resolved Hide resolved
tests/testthat/test-thumbnail.R Outdated Show resolved Hide resolved
tests/testthat/test-thumbnail.R Outdated Show resolved Hide resolved
tests/testthat/test-thumbnail.R Outdated Show resolved Hide resolved
tests/testthat/setup.R Outdated Show resolved Hide resolved
tests/testthat/test-thumbnail.R Outdated Show resolved Hide resolved
R/thumbnail.R Outdated Show resolved Hide resolved
@@ -0,0 +1,88 @@
test_that("set_thumbnail works with local images", {
scoped_experimental_silence()
img_path <- rprojroot::find_package_root_file("tests/testthat/examples/logo.png")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
img_path <- rprojroot::find_package_root_file("tests/testthat/examples/logo.png")
img_path <- "examples/logo.png"

Does this not work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if you put this outside of a test_that block, you should be able to just refer to it in each one rather than having to set it each time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like I replied to the other comment, I haven't futzed with these tests; they are legacy tests adapted to use the new functions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That to say — you're probably right, and I should check!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment was in relation to old integration tests applied verbatim to new thumbnail functions. The integration tests need a more thorough look in general outside of this PR. Jon and I discussed this in an off-GitHub conversation at some point. I'm resolving this conversation to indicate that.

@toph-allen
Copy link
Collaborator Author

toph-allen commented Oct 4, 2024

Something is broken in the integration tests. I was not running those as I was writing these changes.

R/thumbnail.R Outdated Show resolved Hide resolved
R/thumbnail.R Outdated Show resolved Hide resolved
R/thumbnail.R Show resolved Hide resolved
R/thumbnail.R Outdated Show resolved Hide resolved
Co-authored-by: Neal Richardson <[email protected]>
R/thumbnail.R Outdated Show resolved Hide resolved
- handle different types of 404 errors gracefully (depending on connect error code)
- be smarter about file extensions
Comment on lines +30 to +36
# User-specified path (wrong extension)
user_path <- tempfile("thumbnail_", fileext = ".png")
received_path <- get_thumbnail(item, user_path)
received <- readBin(received_path, "raw", n = 8)
expected <- as.raw(c(0x4e, 0x41))
expect_equal(paste0(user_path, ".jpeg"), received_path)
expect_equal(received, expected)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in thumbnail_blah.png.jpeg is that what we want?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems reasonable to me. The converse (substituting rather than appending the correct extension) also seems reasonable.

This only occurs when the user has provided a file path with the incorrect extension. I could see an argument that in this case, it's better to preserve the entire original user-supplied path and just append the correct extension. That's what @nealrichardson did in his suggestion, so I assumed that it was done with that intent.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. IMO I don't think it's worth trying to be even more clever with the filenames.

@toph-allen toph-allen merged commit ccf4f1e into main Oct 8, 2024
19 checks passed
@toph-allen toph-allen deleted the toph/simplify_content_image_functions branch October 8, 2024 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

use v1 content image API (2024.09.0) simplify set_image_*() functions
4 participants